<?php
  require_once(dirname(__FILE__).'/../classes/phpFlickr-2.3.0.1/phpFlickr.php');
  require_once(dirname(__FILE__).'/../config/flickr.php');
  require_once(dirname(__FILE__).'/../db/functions.php');


  //get users
  $all_users_res = db_query("SELECT * FROM users;");

  while($user = mysql_fetch_assoc($all_users_res))
  {
    $user_id = $user['id'];

    //get twitpic username
    $twitpic_username_res = db_query("SELECT twitpic_username FROM twitpic WHERE user_id = ".$user_id.";");
    $twitpic_username_arr = mysql_fetch_array($twitpic_username_res);
    $twitpic_username = $twitpic_username_arr[0];

    //get flicr user id
    $flickr_id_res = db_query("SELECT flickr_id FROM flickr WHERE user_id = ".$user_id.";");
    $flickr_id_arr = mysql_fetch_array($flickr_id_res);
    $flickr_user_id = $flickr_id_arr[0];


    //parse twitpic feed
    $twitpic_feed_url = 'http://twitpic.com/photos/'.$twitpic_username.'/feed.rss';
    $twitpic_feed = new SimpleXmlElement($twitpic_feed_url, LIBXML_NOCDATA, true);

    // if pubDate of latest item is newer than previous item saved in DB

    //get last known feed count
    $last_db_count_res = db_query("SELECT last_feed_count FROM twitpic WHERE user_id = ".$user_id.";");
    $last_db_count_arr = mysql_fetch_array($last_db_count_res);
    $last_db_count = $last_db_count_arr[0];

    if(count($twitpic_feed -> channel -> item) > $last_db_count)
    {
      //more twitpics have been made
      $latest_arr_size = count($twitpic_feed -> channel -> item) - $last_db_count;

      $latest_pics = null;
      for($i = 0; $i < $latest_arr_size; $i++)
      {
        $latest_pics[] = $twitpic_feed -> channel -> item[$i];
      }
    }
    else
    {
      continue; //user hasn't added any more photos...
    }


    if(!is_array($latest_pics))
    {
      continue;
    }
    
    foreach($latest_pics as $latest)
    {
      //extract id from link (eg http://twitpic.com/5zyzo)
      $pic_id = preg_replace('$http\:\/\/twitpic\.com\/$', '', $latest -> link);
  
      //get title
      $pic_title_a = explode(" ", $latest -> title);
      $pic_title_a[0] = $pic_title_a[1] = '';
      $pic_title = implode($pic_title_a);
      $pic_title = stripslashes($pic_title);
      $pic_title = preg_replace('$\/$', '', $pic_title);
  
      //get picture
      exec('wget http://twitpic.com/show/thumb/'.$pic_id.'.jpg -O image_cache/'.$pic_title.'.jpg -a log/wget_log');
  
      $api = new phpFlickr(API_KEY, API_SECRET, API_DIE_ON_ERROR);

      //get flickr auth string
      $flickr_auth_res = db_query("SELECT flickr_auth_string FROM flickr WHERE user_id = ".$user_id.";");
      $flickr_auth_arr = mysql_fetch_array($flickr_auth_res);
      $flickr_auth_key = $flickr_auth_arr[0];

      $api -> setToken($flickr_auth_key);

   		//if twitpic set doesn't exist - create it
   		//$flickr_user_id = '30774908@N06';
   		$sets = $api -> photosets_getList($flickr_user_id);
   		$has_set = false;
    
   		foreach($sets['photoset'] as $set)
   		{
   			if($set['title'] == 'Twitpic Photos')
   			{
   				$has_set = true;
   				$photoset_id = $set['id'];
   				break;
   			}
   		}
  
      //call flickr api to send picture
      $result = $api -> sync_upload('image_cache/'.$pic_title.'.jpg', 'Twitpic: '.$latest -> title, $latest -> link, null, 1);
  
      if($result)
      {
     		if(!$has_set) //create the set if its not there
    		{
     			$photoset_id = $api -> photosets_create('Twitpic Photos', 'Automatically created by the Flickpic application for your Twitpic photos', $result);
        }
        else
        {
   	  		//add photo to the existing set
    			$api -> photosets_addPhoto($photoset_id, $result);
        }
   
        //remove file from image_cache/
        exec('rm image_cache/'.$pic_title.'.jpg');

        echo $result."\n";
      }

    }//end foreach photo

    //update database to include new pubDate
    db_query("UPDATE twitpic SET last_feed_count = '".count($twitpic_feed -> channel -> item)."' WHERE user_id = ".$user_id." LIMIT 1;");

  } //end user while
?>
